$(function() {

	function message(msg) {
		var re = /^\d{5,}$/;
		return re.test(msg)
	}

	function checkisPhoneNum(num) {
		var re = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9]|16[0-9]|19[0-9])\d{8}$/;
		return re.test(num)
	}
	//验证用户名的
	function checkisUserName(name) {
		var re = /^[0-9a-z]{6,16}$/;
		return re.test(name);
	}
	//
	function checkIsEmail(email) {
		var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
		return re.test(email)
	}

	function checkIsPassword(pw) {
		if(pw.length > 0) {
			var re = /[\u2E80-\u9FFF]+/;
			return !re.test(pw);
		} else {
			return false;
		}
	}

	$('input').focus(function() {
		$(this).toggleClass('act-clazz')
	})
	$('input').blur(function() {
		$(this).toggleClass('act-clazz')
	})

	var login = document.getElementById('login-left');
	var reg = document.getElementById('login-right');

	var loginTips = login.getElementsByClassName('tips');
	var regTips = reg.getElementsByClassName('tips');

	var loginIpt = login.getElementsByTagName('input');
	var regIpt = reg.getElementsByTagName('input');
	var loginBtn = login.getElementsByClassName('loginBtn')[0];
	var regBtn = reg.getElementsByClassName('regBtn')[0];

	//reg
	var isUserName = false;
	var pwd1 = false;
	var pwd2 = false;
	var isPhone = false;
	//login
	var UserName = false;
	var passWord = false;

	//login
	for(var i = 0; i < loginIpt.length; i++) {
		if(i == 0) {
			loginIpt[i].onkeyup = function() {
				if(checkisUserName(this.value)) {
					UserName = true;
					loginTips[0].innerHTML = '√';
					loginTips[0].style.color = '#71c86f';
				} else {
					UserName = false;
					loginTips[0].innerHTML = '输入有误';
					loginTips[0].style.color = '#E31436';
				}
			}

		} else if(i == 1) {
			loginIpt[i].onkeyup = function() {
				if(checkIsPassword(this.value)) {
					passWord = true;
					this.nextElementSibling.innerHTML = '√';
					this.nextElementSibling.style.color = '#71c86f';
					//密码强度判断
					//console.log(this.value)

				} else {
					passWord = false;
					this.nextElementSibling.innerHTML = '输入有误';
					this.nextElementSibling.style.color = '#E31436';
				}
			}
		}
	}

	loginBtn.onclick = function() {
		if(UserName && passWord) {
			alert('登录成功')
			window.open('../index.html')
		} else {
			alert('登录失败')
		}
	}

	//reg
	for(var i = 0; i < regIpt.length; i++) {
		if(i == 0) {
			regIpt[i].onkeyup = function() {
				if(checkisUserName(this.value)) {
					isUserName = true;
					regTips[0].innerHTML = '√';
					regTips[0].style.color = '#71c86f';
				} else {
					isUserName = false;
					regTips[0].innerHTML = '输入有误';
					regTips[0].style.color = '#E31436';
				}
			}

		} else if(i == 1) {
			regIpt[i].onkeyup = function() {
				if(checkIsPassword(this.value)) {
					pwd1 = true;
					this.nextElementSibling.innerHTML = '√';
					this.nextElementSibling.style.color = '#71c86f';
					//密码强度判断
					//console.log(this.value)

				} else {
					pwd1 = false;
					this.nextElementSibling.innerHTML = '输入有误';
					this.nextElementSibling.style.color = '#E31436';
				}
			}
		} else if(i == 2) {
			regIpt[i].onkeyup = function() {
				if(this.value == regIpt[1].value) {
					pwd2 = true;
					this.nextElementSibling.innerHTML = '√';
					this.nextElementSibling.style.color = '#71c86f';
					//密码强度判断
					//console.log(this.value)

				} else {
					pwd2 = false;
					this.nextElementSibling.innerHTML = '输入有误';
					this.nextElementSibling.style.color = '#E31436';
				}
			}
		} else if(i == 3) {
			regIpt[i].onkeyup = function() {
				if(checkisPhoneNum(this.value)) {
					isPhone = true;
					this.nextElementSibling.innerHTML = '√';
					this.nextElementSibling.style.color = '#71c86f';
					//密码强度判断
					//console.log(this.value)

				} else {
					isPhone = false;
					this.nextElementSibling.innerHTML = '输入有误';
					this.nextElementSibling.style.color = '#E31436';
				}
			}
		}
	}
	regBtn.onclick = function() {
		if(isUserName && pwd1 && pwd2 && isPhone) {
			alert('注册成功')
			reg.style.display = 'none';
		} else {
			alert('注册失败')
		}
	}

	function invokeSettime(obj) {
		var countdown = 60;
		settime(obj);

		function settime(obj) {
			if(countdown == 0) {
				$(obj).attr("disabled", false);
				$(obj).text("获取验证码");
				countdown = 60;
				return;
			} else {
				$(obj).attr("disabled", true);
				$(obj).css("opacity",'.5')
				$(obj).text(countdown +"S重新发送");
				countdown--;
			}
			setTimeout(function() {
				settime(obj)
			}, 1000)
		}
	}
	$(".getCode").click(function(){
		var disabled = $(".getCode").attr("disabled");
		console.log(disabled);
		if(disabled){
			return false;
		}
		if($("#mobile").val() == "" || isNaN($("#mobile").val()) || $("#mobile").val().length != 11){
			alert("请输入正确的手机号");
			return false;
		}
		console.log($("#mobile").val())
		$.ajax({
			type:"get",
			url:"",
			async:false,
			data:{t:$("#mobile").val()},
			dataType:"json",
			success:function(data){
				console.log(data);
				settime();
			},
			error:function(err){
				console.log(err);
			}
		});
		
		
		new invokeSettime(".getCode")
	})
	

})